package com.gxa.modules.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.RespResult;
import com.gxa.modules.dto.RetrieveDTO;
import com.gxa.modules.mapper.CustomerMapper;
import com.gxa.modules.mapper.RetrievetMapper;
import com.gxa.modules.service.RetrieveService;
import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.expr.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.logging.Handler;

@Service
@Slf4j
public class RetrieveServiceImpl implements RetrieveService {
    @Autowired
    private RetrievetMapper retrievetMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public RespResult findRetrieveList(Integer currentPage, Integer pageSize, String type, String value) {
        log.info("{}查询回收站信息",new Date());
        Page<RetrieveDTO> page=new Page<>(currentPage,pageSize);

        Map map=new HashMap<>();
        Page<RetrieveDTO> retrieveDTOList=null;
        if("all".equals(type)){
            retrieveDTOList=retrievetMapper.findAll(page,value);
        }else if("day".equals(type)){
            retrieveDTOList=retrievetMapper.findByDay(page,value);
        }else if("week".equals(type)){
            retrieveDTOList=retrievetMapper.findByWeek(page,value);
        }else if("month".equals(type)){
            retrieveDTOList=retrievetMapper.findByMouth(page,value);
        }else if(type==null){
            return RespResult.error("数据错误");
        }
        if(retrieveDTOList!=null) {
            map.put("total", retrieveDTOList.getTotal());
            map.put("retrieve", retrieveDTOList.getRecords());
        }
        return RespResult.success(map);
    }

    @Override
    public RespResult returnC(Integer[] customerIds) {
        log.info("{}还原客户信息",new Date());
        int row=retrievetMapper.updateC(customerIds);
        if(row>0){
            return RespResult.success();
        }else {
            return RespResult.error("还原失败");
        }
    }

    @Override
    public RespResult returnSea(Integer[] customerIds) {
        log.info("{}将客户转入公海",new Date());
        int row=retrievetMapper.returnSea(customerIds);
        if(row>0){
            return RespResult.success();
        }else {
            return RespResult.error("转入公海失败");
        }
    }

    @Override
    public RespResult delRetrieve(Integer[] customerIds) {
        log.info("{}彻底删除客户信息",new Date());
        int row=customerMapper.deleteBatchIds(Arrays.asList(customerIds));
        if(row>0){
            return RespResult.success();
        }else {
            return RespResult.error("彻底删除失败");
        }
    }
}
